<?php
/**
 * 资讯管理页面
 */
// include_once  'tools/simplehtmldom/simple_html_dom.php';
//include_once  'core/OpenSociax/functions.inc.php';
class NewsAction extends Action{
	//显示资讯类型
	public function showNewsCategory(){
		$pid = isset($_REQUEST["pid"])?dt($_REQUEST["pid"]):0;
		$category = M("News_Category");
		$data = $category->where("pid = $pid")->select();
		$value = ["code" =>"success","data"=>$data];
		header("Content-Type: text/json");
		die(et($value));
		
	}
	
	public function showDuanzi(){
		//分页设置
		$pagecount=isset($_REQUEST['"pagecount'])?dt($_REQUEST['pagecount']):"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?dt($_REQUEST['pagenum']):"1")-1);//当前页数
		$sql ="select id,title,content,cTime from  wz_vduanzi where isImage is null limit $pagenum,$pagecount";
		$data = M("Vduanzi")->query($sql);
		$value = ["code" =>"success","data"=>$data];
		header("Content-Type: text/json");
		die(et($value));
	}
	public function showPic(){
		//分页设置
		$pagecount=isset($_REQUEST['"pagecount'])?dt($_REQUEST['pagecount']):"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?dt($_REQUEST['pagenum']):"1")-1);//当前页数
		
		$sql = "select  id,image,cTime from wz_vduanzi where isImage = 1 limit $pagenum,$pagecount";
		$data = M("Vduanzi")->query($sql);
		$value = ["code" =>"success","data"=>$data];
		header("Content-Type: text/json");
		die(et($value));
	}
	//显示相应类型的资讯列表
	public function showNewsList(){
		$type_id = dt($_REQUEST["type_id"]);
		
		if (!isset($type_id)){
			$res = ["code"=>"wrong","massage"=>"参数不能为空！"];
			header("Content-Type: text/json");
			die(et($res));
		}
		//分页设置
		$pagecount=isset($_REQUEST['pagecount'])?dt($_REQUEST['pagecount']):"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?dt($_REQUEST['pagenum']):"1")-1);//当前页数
		
		$data = M("News")->where("type_id = $type_id")->limit($pagenum,$pagecount)->select();
		if ($data != false){
			$value=["code"=>"success","data"=>$data];
		}else{
			if(mysql_affected_rows($data)){
				$value=["code"=>"success","data"=>null];
			}
			$value=["code"=>"wrong","data"=>M("News")->getDbError()];
		}
		header("Content-Type: text/json");
		die(et($value));
	}
	//详细单个详细资讯 返回详情链接
	public function showNews(){
		$news_id = dt($_REQUEST["news_id"]);
		$value = ["code"=>'success',"url"=>C("__NEWSURL__").$news_id];
		header("Content-Type: text/json");
		die(et($value));
	}
	public function NewsWap(){
		$news_id = dt($_REQUEST["news_id"]);
		$news_data = M("News")->where("news_id = $news_id")->find();
// 		dump($news_data);
		$image = D('attach')->where('attach_id='.$news_data['image'])->find();
		$image_url = $image['save_path'].$image['save_name'];
		$news_data['image'] = getImageUrl($image_url);
		$news_data["created"] = date("Y年m月d日");
		
		$this->assign("news",$news_data);
		$this->display("newswap");
	}
	
	// 采集数据
		//null
	function captureData(){
		$url = 'http://api.gaoqu001.com/gaoqu/duanzi?pageSize=1000&page=1';
		if ($_REQUEST['is_pic']) {
			$url = 'http://api.gaoqu001.com/gaoqu/pic?page=2&pageSize='.$_REQUEST['pageSize'];
		}
		$list = $this->captureDuanzi($url);
		foreach ($list as $value) {
			$this->Vduanzi->saveCaptureData($value);
		}
		return $list;
	}
		//null
	function captureDuanzi($url='http://api.gaoqu001.com/gaoqu/pic?page=1&pageSize=20') {
		$Vduanzi = M("Vduanzi", 'vduanzi');
		$html = file_get_contents("http://www.yidianzixun.com/home?page=channel&keyword=%E7%A7%91%E6%8A%80");
		
		//echo $html;
		$resArr = array();
		$resArr = json_decode($html);
		dump($resArr);
		
		$items = $resArr->list;
		dump($items);exit;
		$count = $resArr->count;
		$page = $resArr->page;
		$dataList = array();
		for ($i = 0; $i < count($items); $i++) {
			$data['other_app'] = 'duanzibao';
			$data['other_id'] = $items[$i]->id;
			if ($this->Vduanzi->isHaved($data['other_id'])) {
				//echo 'haved <br>';
				continue;
			}
			$data['title'] = $items[$i]->title;
			$data['content'] = $items[$i]->content;
			$data['cTime'] = $items[$i]->sourcePublishDate;
			$data['status'] = 1;
			$data['isImage'] = 0;
			if ($items[$i]->image) {
				//continue;
				$image = $items[$i]->image;
				if (strstr($image, 'pic.qiushibaike.com') !== false) {
					continue;
				}
				//				$htmlImg = file_get_html($url);
				//				$useFull = $htmlImg->find('div div[class=thumb] a img', 0);
				//				if ($useFull && $htmlImg) {
				//					$image = t($useFull->src);
				//				}
				$imageUrl = $this->GrabImage($image);
				if (!$imageUrl) continue;
				if ($Vduanzi->isTheSameImage($image, $data['other_app'] )) continue;
				$data['image'] = $image;
				$data['imageWidth'] = $items[$i]->imageWidth;
				$data['imageHeight'] = $items[$i]->imageHeight;
				$data['isImage'] = 1;
			}
			$dataList[] = $data;
		}
		return $dataList;
	}
	//有数据
	function captureDuanziShou () {
		$url="http://duanzi.duowan.com/index.php?r=duanzi/topicList&page=1&pageSize=10&sort=2";
		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		$html = file_get_contents($url);
		echo $html;
		$resArr = array();
		$resArr = json_decode($html);
		dump($resArr);exit;
		$resultArr = $resArr->result;
		$items = $resultArr->list;
		for ($i = 0; $i < count($items); $i++) {
			$data['other_app'] = 'DuanziShou1';
			$data['other_id'] = $items[$i]->art_id;
			if ($this->Vduanzi->isHaved($data['other_id'], $data['other_app'])) {
				//echo 'haved <br>';
				continue;
			}
			$data['content'] = $items[$i]->content;
			$data['cTime'] = date("Y-m-d H:i:s", $items[$i]->created);
			$imglist = $items[$i]->imagelist;
			$data['image'] = $imglist[0]->large;
			$data['imageWidth'] = $imglist[0]->width;
			$data['imageHeight'] = $imglist[0]->height;
			$data['isImage'] = 1;
			$data['status'] = 1;
			$this->Vduanzi->saveCaptureData($data);
		}
	}
	
	function captureMoreJuyouqu($num=10,$beginNumber=3000000) {
		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		$beforUrl = 'http://www.juyouqu.com/';
		$maxid=$this->Vduanzi->maxOther_id('juyouqu');
		$beginNumber = $maxid;
		$hadTadeOne = false;
		for ($i=0; $i<$num; $i++) {
			$beginNumber++;
			$url = $beforUrl.$beginNumber;
			$data = $this->captureDuanzi($url);
			if (!$data) return '0';
			$data['other_app'] = 'juyouqu';
			$data['other_id'] = $beginNumber;
			if ($this->Vduanzi->isHaved($data['other_id'], $data['other_app'] )) {
				continue;
			}
			$data['title'] = $data['title'];
			$data['content'] = $data['title'];
			$data['cTime'] = date("Y-m-d H:i:s", time());
			$imageUrl = GrabImage($data['image']);
			if (!$imageUrl) continue;
			$data['image'] = $imageUrl;
			$filepath = 'http://'.$_SERVER['HTTP_HOST'].$imageUrl;
			$imgsi = getimagesize($filepath);
			$data['imageWidth'] = $imgsi[0];
			$data['imageHeight'] =  $imgsi[1];
			$data['isImage'] = 1;
			$data['status'] = 1;
			$this->Vduanzi->saveCaptureData($data);
			$hadTadeOne = true;
		}
		if (!$hadTadeOne) $this->Vduanzi->appendMaxOhter_id($num, $data['other_app']);
		return "finish";
	}
	
	function captureSexBeauty($url='http://112.124.107.196/picfun/weibo_list.php?apiver=10000&category=weibo_girls&page=0&page_size=10&max_timestamp=-1&latest_viewed_ts=1418687460&vip=1&platform=iphone&appver=1.0.3&udid=051BD8FF-1BAC-467A-A9E0-78B6E63F5654') {
		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		$html = file_get_contents($url);
		echo $html;
		$resArr = array();
		$resArr = json_decode($html);
		dump($resArr);exit;
		$items = $resArr->items;
		for ($i = 0; $i < 10; $i++) {
			$data['other_app'] = 'nhtpdz_weibo_girls';
			$data['other_id'] = $items[$i]->wid;
			if ($this->Vduanzi->isHaved($data['other_id'], $data['other_app'])) {
				//echo 'haved <br>';
				continue;
			}
// 			$data['cTime'] = date("Y-m-d H:i:s", $items[$i]->update_time);
// 			$data['image'] = $items[$i]->wpic_middle;;
// 			$data['imageWidth'] = $items[$i]->wpic_m_width;
// 			$data['imageHeight'] = $items[$i]->wpic_m_height;
// 			$data['isImage'] = 3;
// 			$data['status'] = 1;
// 			$this->Vduanzi->saveCaptureData($data);
		}
	}
	
	function captureHao123($num=10,$beginNumber=7900970) {
		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		$beforUrl = 'http://pic.hao123.com/image/meinv/';
		$maxid=$this->Vduanzi->maxOther_id('hao1231');
		//$beginNumber = $maxid? $maxid:$beginNumber;
		$hadTadeOne = false;
		for ($i=0; $i<$num; $i++) {
			$beginNumber++;
			$url = $beforUrl.$beginNumber;
			$html = file_get_contents($url);
			if (!$html->find('div[class=section-articles] div  a img', 0)) continue;
			$useFull = $html->find('div[class=section-articles] div  a img', 0);
			$data['title'] = t($useFull->title);
			if (!t($useFull->src)) continue;
			$data['image'] = t($useFull->src);
			$data['imageWidth'] = t($useFull->picwidth);
			$data['imageHeight'] = t($useFull->picheight);
			$data['other_app'] = 'hao1231';
			$data['other_id'] = $beginNumber;
			if ($this->Vduanzi->isHaved($data['other_id'], $data['other_app'] )) {
				continue;
			}
			if ($this->Vduanzi->isTheSameImage($data['image'], $data['other_app'] )) continue;
			$data['content'] = $data['title'];
			$data['cTime'] = date("Y-m-d H:i:s", time());
			if (!$data['image']) continue;
			$data['isImage'] = 3;
			$data['status'] = 1;
			$this->Vduanzi->saveCaptureData($data);
			$hadTadeOne = true;
		}
		dump($data);exit;
		if (!$hadTadeOne) $this->Vduanzi->appendMaxOhter_id($num, $data['other_app']);
		return "finish";
	}
	//meinv
	public function captureMM131(){
	
		$Vduanzi = M("Vduanzi");
		
		$beforUrl = 'http://pic.yesky.com/453/35424953.shtml';
		
		$html = file_get_contents($beforUrl);
// 		$html = str_get_html($beforUrl);
		
// 		echo $html;
		dump($html);
		echo "sdlkfjas";
		preg_match_all('/<img[^>]*>/i', $html, $match);
		dump($match);exit;

	
	
	}
	//ok
	public function captureYidian(){
		
		$Vduanzi = M("Vduanzi");
		$beforUrl = 'http://www.yidianzixun.com/home?page=channel&keyword=%E7%BE%8E%E5%A5%B3';
		$html = file_get_contents($beforUrl);
		//dump($html);exit;
		preg_match_all('/<img[^>]*>/i', $html, $match);
		dump($match);
		$a = array();
		for($i=0;$i<count($match[0]);$i++){
			//echo $match[0][$i];
			preg_match('/<img.*?src="(.*?)".*?>/is',$match[0][$i],$img);
			$a[$i] = $img[1];
			if(strstr($img[1],"http")){
				$res = $this->GrabImage($img[1]);
				$Vduanzi->isImage = 1;
				$Vduanzi->other_app = "yidian";
				$Vduanzi->image = $res;
				if($Vduanzi->add()){
					echo "ok".$i."张";
				}else{
					echo "no".$i."张";
				}
			}else{
				echo "不符合".$i;
			}
			
		}
		
		
	}
	public function otherthink(){

		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		$beforUrl = 'http://www.yidianzixun.com/home?page=channel&keyword=%E7%BE%8E%E5%A5%B3';
		$html = file_get_contents($beforUrl);
		//echo $html;
		// 		echo "asdf";
		// 		if (!$html->find('div[class=section-articles] div  p a img', 0)) {
		// 			echo "sdfasdfa";
		// 			continue;
		// 		}
		// 		echo "asdf";
		// 		$useFull = $html->find('div[class=section-articles] div  p a img', 0);
		// 		echo "asdf";
		//preg_match('|<div class="section-articles" id="yui_3_5_0_1_1445499944019_136">(.*?)<\/div>|i',$html,$m);
		//preg_match('/<div[^>]*class="article article-pic"[^>]*>(.*?) <//div>/si',$html,$match);
		preg_match_all('/<img[^>]*>/i', $html, $match);
		dump($match);
		$img = array();
		for($i=0;$i<count($match[0]);$i++){
			//echo $match[0][$i];
			preg_match('/<img.*?src="(.*?)".*?>/is',$match[0][$i],$img[$i]);
			$a[$i] = $img[$i][1];
		}
		
		dump($img);
		dump($a);
	}
	public function tete(){
		$ch=curl_init();
		$timeout = 5;
		curl_setopt($ch, CURLOPT_URL, "http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=%E7%BE%8E%E5%A5%B3#z=0&pn=&ic=0&st=-1&face=0&s=0&lm=-1");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		
		$file_content = curl_exec($ch);
		curl_close($ch);
		//dump($file_content);
		preg_match_all('/<img[^>]*>/i', $file_content, $match);
		dump($match);exit;
	}
	public function tsts(){
		$url = 'http://pic.yesky.com/453/35424953.shtml';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); //加入重定向处理
		$output = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);
		dump($info);
		dump($output);
		
	}
	//ok
	public function captureWaduanzi(){

		$this->Vduanzi = M("Vduanzi", 'vduanzi');
		for($n=1;$n<10;$n++){
			$beforUrl = 'http://www.waduanzi.com/joke/page/'.$n;
			$html = file_get_contents($beforUrl);
			
			$regex4="/<div class=\"panel panel20 post-item post-box\".*?>.*?<div class=\"site-bg item-shadow\">/ism";
			
			$regex5="/<h2 class=\"item-title\".*?>.*?<div class=\"item-toolbar\">/ism";
			preg_match_all($regex5,$html,$match);
			
			$regex6="/<h2 class=\"item-title\".*?>.*?<\/a>/ism";
			$regex7 ="/<div class=\"item-content\".*?>.*?<\/div>/ism";
			$array=array();
			for($i=0;$i<count($match[0]);$i++){
				preg_match($regex6,$match[0][$i],$title);
				preg_match($regex7,$match[0][$i],$content);
				
				$res=explode(">", $title[0]);
				$array[$i]["title"]=substr($res[2],0,-3);
				$array[$i]["content"]=str_replace(array("<div class=\"item-content\">","</div>","<p>","</p>","<br />"),"",$content[0]);
			}
			if(!isset($array)){
				continue;
			}
			for($k=0;$k<count($array);$k++){
				
				
				if(is_int(strpos($array[$k]["content"],"<"))){
					echo "no$n页-第".$k."条"."<br/>";
					continue;
				}else {
					
					$duanzi = M("Vduanzi");
					if ($duanzi->where("title = ".$array[$k]["title"]." and content = ".$array[$k]["content"])->select()){
						echo "already$n页-第".$k."条"."<br/>";
						continue;
					}
					$duanzi->title = $array[$k]["title"];
					$duanzi->content = $array[$k]["content"];
					$duanzi->other_app = "waduanzi";
					$duanzi->cTime= time();
					
					if($duanzi->add()){
						echo "ok".$n."页-第".$k."条"."<br/>";
					}else{
						echo "no$n页-第".$k."条"."<br/>";
					}
				}
			}
			unset($array);
			
		}
		
	}

	//
	// Function: 获取远程图片并把它保存到本地
	//
	//
	//   确定您有把文件写入本地服务器的权限
	//
	//
	// 变量说明:
	// $url 是远程图片的完整URL地址，不能为空。
	// $filename 是可选变量: 如果为空，本地文件名将基于时间和日期
	// 自动生成.
	function GrabImage($url,$filename="") {
		if($url==""):return false;endif;
		if($filename=="") {
			$ext=strrchr($url,".");
			if($ext!=".gif" && $ext!=".jpg"):return false;endif;
			$filename=date("dMYHis").rand('1000', '10000').$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);
		 
		$filepathdate = date("Ymd",time());
		$filepath1 = "/data/upload/sexBueaty/yidian/".$filepathdate.'/';
		$filepath = getcwd().$filepath1;
		if(!is_dir($filepath)){
			mkdir($filepath,0777, true);
		}
	
		if (!$handle = fopen($filepath.$filename, 'a')) {
			print "不能打开文件 $filename <br>";
			exit;
		}
		// 将$somecontent写入到我们打开的文件中。
		$res = fwrite($handle, $img);
		if (!$res) {
		print "不能写入到文件 $filename <br>";
			exit;
		}
		//print "成功地将 $filepath 写入到文件$filename <br>";
	
			fclose($handle);
			return $filepath1.$filename;
	}

}